package com.cqjtu.tto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.tto.dao.mapper.PermissionsMapper;
import com.cqjtu.tto.dao.pojo.Permissions;
import com.cqjtu.tto.service.PermissionService;
import com.cqjtu.tto.vo.ErrorCode;
import com.cqjtu.tto.vo.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author：monkey-L
 * @description:
 * @date: 19:18 2021/12/11
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionsMapper permissionsMapper;

    @Override
    @Transactional
    public Results permissionAdd(String permissionsName) {
        LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
        permissionsWrapper.eq(Permissions::getPermissionsName,permissionsName).last("limit 1");
        Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
        if (permissions != null) {
            return Results.fail(ErrorCode.PERMISSIONS_EXIST.getCode(),ErrorCode.PERMISSIONS_EXIST.getMsg());
        }
        Permissions permissions1 = new Permissions();
        permissions1.setPermissionsId(System.currentTimeMillis());
        permissions1.setPermissionsName(permissionsName);
        permissions1.setPermissionsDeleted(0);
        permissionsMapper.insert(permissions1);
        return Results.success("添加权限成功！");
    }

    @Override
    @Transactional
    public Results permissionDelete(String permissionsName) {
        LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
        permissionsWrapper.eq(Permissions::getPermissionsName,permissionsName).last("limit 1");
        Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
        if (permissions == null) {
            return Results.fail(ErrorCode.PERMISSIONS_NOT_EXIST.getCode(),ErrorCode.PERMISSIONS_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<Permissions> permissionsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        permissionsLambdaQueryWrapper.eq(Permissions::getPermissionsId,permissions.getPermissionsId());
        permissionsMapper.delete(permissionsLambdaQueryWrapper);
        return Results.success("删除权限成功！");
    }

    @Override
    @Transactional
    public Results permissionUpdate(String oldPermissionsName, String newPermissionsName) {
        LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
        permissionsWrapper.eq(Permissions::getPermissionsName,oldPermissionsName).last("limit 1");
        Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
        if (permissions == null) {
            return Results.fail(ErrorCode.PERMISSIONS_NOT_EXIST.getCode(),ErrorCode.PERMISSIONS_NOT_EXIST.getMsg());
        }
        Permissions permissions1 = new Permissions();
        permissions1.setPermissionsDeleted(0);
        permissions1.setPermissionsId(permissions.getPermissionsId());
        permissions1.setPermissionsName(newPermissionsName);
        LambdaQueryWrapper<Permissions> permissionsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        permissionsLambdaQueryWrapper.eq(Permissions::getPermissionsId,permissions.getPermissionsId());
        permissionsMapper.update(permissions1,permissionsLambdaQueryWrapper);
        return Results.success("更新权限信息成功！");
    }

    @Override
    @Transactional
    public Results permissionSelectOne(String permissionsName) {
        LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
        permissionsWrapper.eq(Permissions::getPermissionsName,permissionsName).last("limit 1");
        Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
        if (permissions == null) {
            return Results.fail(ErrorCode.PERMISSIONS_NOT_EXIST.getCode(),ErrorCode.PERMISSIONS_NOT_EXIST.getMsg());
        }
        return Results.success(permissions);
    }

    @Override
    @Transactional
    public Results permissionSelectList() {
        LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
        List<Permissions> permissions = permissionsMapper.selectList(permissionsWrapper);
        if (permissions == null) {
            return Results.success("暂无权限信息");
        }
        return Results.success(permissions);
    }
}
